package httpclient

import (
	"gitee.com/sdynasty/clever/cLog"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
)

func HttpGet(url string) (back []byte, err error) {
	resp, err := http.Get(url)
	if err != nil {
		clog.LogLogic().Warnf("http get error. url:%s, err:%v", url, err)
		return
	}

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		clog.LogLogic().Warnf("http get ioutil.ReadAll error. url:%s, err:%v", url, err)
		return
	}
	back = body
	clog.LogLogic().Debugf("http get url:%s, body:%s", url, string(back))
	return
}

func HttpPost(url string, data string) {
	resp, err := http.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data))
	if err != nil {
		clog.LogLogic().Warnf("http post error. url:%s, err:%v", url, err)
		return
	}

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		clog.LogLogic().Warnf("http post ioutil.ReadAll error. url:%s, err:%v", url, err)
		return
	}

	clog.LogLogic().Debugf("http post url:%s, body:%s", url, string(body))
}

func HttpPostForm(url string, values url.Values) {
	resp, err := http.PostForm(url, values)
	if err != nil {
		clog.LogLogic().Warnf("http postForm error. url:%s, err:%v", url, err)
		return
	}

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		clog.LogLogic().Warnf("http postForm ioutil.ReadAll error. url:%s, err:%v", url, err)
		return
	}

	clog.LogLogic().Debugf("http postForm url:%s, body:%s", url, string(body))
}